
package com.jf.cloud.service.sysmanager.remote;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.common.util.StringUtil;
import com.jf.cloud.service.sysmanager.constant.sms.SmsConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgBasePropsConfigService;
import com.jf.cloud.service.sysmanager.entity.PubgBasePropsConfig;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.handler.PropConfigHandler;
import com.jf.cloud.service.sysmanager.support.entity.Result;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * 基础属性配置控制器
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Controller
@RequestMapping("/basePropsConfig")
public class PubgBasePropsConfigController extends BaseController {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBasePropsConfigController.class);

	/**
	 * 基础属性配置服务
	 */
	@Resource
	private IPubgBasePropsConfigService pubgBasePropsConfigService;

	/**
	 * 基础属性配置列表页面
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:45:36
	 */
	@ResponseBody
	@RequestMapping("/listPubgBasePropsConfig")
	public AjaxPageData<PubgBasePropsConfig> listPubgBasePropsConfig(OPaging<PubgBasePropsConfig> page, HttpServletRequest request) {
		AjaxPageData<PubgBasePropsConfig> dataPage = new AjaxPageData<PubgBasePropsConfig>();
		try {
			SysStaff sysStaff = getLoginUser(request);
			if (sysStaff == null) {
				return dataPage;
			}
			if (StringUtil.isNull(page.getWm().get("DEL_FLAG") + "")) {
				page.getWm().put("DEL_FLAG", "1");
			}
			dataPage = getAjaxPageData(pubgBasePropsConfigService.findPage(page));
		} catch (Exception e) {
			log.error("获取基础属性配置列表页面出错！", e);
		}
		return dataPage;
	}

	/**
	 * 新增基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:45:28
	 */
	@ResponseBody
	@RequestMapping("/addPubgBasePropsConfig")
	public Result<String> addPubgBasePropsConfig(PubgBasePropsConfig pubgBasePropsConfig, HttpServletRequest request) {
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("维护基础属性配置出错了");
		try {
			SysStaff sysStaff = getLoginUser(request);
			if (sysStaff == null) {
				result.setCode(-2);
				result.setMessage("请重新登陆");
				return result;
			}
			if (StringUtil.isNull(pubgBasePropsConfig.getCfgKey())) {
				result.setMessage("配置KEY为空！");
				return result;
			}
			if (StringUtil.isNull(pubgBasePropsConfig.getCfgValue())) {
				result.setMessage("配置VALUE为空！");
				return result;
			}
			if (this.pubgBasePropsConfigService.checkConfigIsExist(pubgBasePropsConfig)) {
				result.setMessage("配置KEY已存在！");
				return result;
			}
			pubgBasePropsConfig.setOptId(sysStaff.getRowId());
			pubgBasePropsConfig.setOptName(sysStaff.getStaffAccount());
			pubgBasePropsConfig.setOptTime(DateUtil.getCurrentDateTime());
			this.pubgBasePropsConfigService.savePubgBasePropsConfig(pubgBasePropsConfig);
			result.setCode(0);
			result.setMessage("维护基础属性配置成功！");
		} catch (Exception e) {
			log.error("维护基础属性配置出错！", e);
		}
		return result;
	}
	
	/**
	 * 获取指定基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午4:08:21
	 */
	@ResponseBody
	@RequestMapping("/getInfo")
	public PubgBasePropsConfig getInfo(Long rowId, HttpServletRequest request) {
		return this.pubgBasePropsConfigService.findByPK(rowId);
	}
	
	/**
	 * 修改基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:45:28
	 */
	@ResponseBody
	@RequestMapping("/updatePubgBasePropsConfig")
	public Result<String> updatePubgBasePropsConfig(PubgBasePropsConfig pubgBasePropsConfig, HttpServletRequest request) {
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("修改基础属性配置出错了");
		try {
			SysStaff sysStaff = getLoginUser(request);
			if (sysStaff == null) {
				result.setCode(-2);
				result.setMessage("请重新登陆");
				return result;
			}
			if (StringUtil.isNull(pubgBasePropsConfig.getCfgKey())) {
				result.setMessage("配置KEY为空！");
				return result;
			}
			if (StringUtil.isNull(pubgBasePropsConfig.getCfgValue())) {
				result.setMessage("配置VALUE为空！");
				return result;
			}
			if (this.pubgBasePropsConfigService.checkConfigIsExist(pubgBasePropsConfig)) {
				result.setMessage("配置KEY已存在！");
				return result;
			}
			pubgBasePropsConfig.setOptId(sysStaff.getRowId());
			pubgBasePropsConfig.setOptName(sysStaff.getStaffAccount());
			pubgBasePropsConfig.setOptTime(DateUtil.getCurrentDateTime());
			this.pubgBasePropsConfigService.updatePubgBasePropsConfig(pubgBasePropsConfig);
			result.setCode(1);
			result.setMessage("修改基础属性配置成功！");
		} catch (Exception e) {
			log.error("修改基础属性配置出错！", e);
		}
		return result;
	}
	
	/**
	 * 删除/恢复基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月4日 下午2:45:20
	 */
	@ResponseBody
	@RequestMapping("/updateDelFlag")
	public Result<String> updateBasePropsConfigDelFlag(PubgBasePropsConfig pubgBasePropsConfig, HttpServletRequest request) {
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("维护基础属性配置出错了");
		try {
			SysStaff sysStaff = getLoginUser(request);
			if (sysStaff == null) {
				result.setCode(-2);
				result.setMessage("请重新登陆");
				return result;
			}
			if (!"1".equals(pubgBasePropsConfig.getDelFlag()) 
					&& !"0".equals(pubgBasePropsConfig.getDelFlag())) {
				result.setMessage("delFlag不合法！");
				return result;
			}
			pubgBasePropsConfig.setOptId(sysStaff.getRowId());
			pubgBasePropsConfig.setOptName(sysStaff.getStaffAccount());
			pubgBasePropsConfig.setOptTime(DateUtil.getCurrentDateTime());
			this.pubgBasePropsConfigService.updateDelFlag(pubgBasePropsConfig);
			result.setCode(0);
			result.setMessage("维护基础属性配置成功！");
		} catch (Exception e) {
			log.error("维护基础属性配置出错！", e);
		}
		return result;
	}

	/**
	 * 获取基础属性配置
	 * 
	 * @author 朱建文 创建时间：2018年5月5日 上午10:42:01
	 */
	@ResponseBody
	@RequestMapping("/showBasePropsConfig")
	public Result<Map<String, Object>> getBasePropsConfig(Long rowId, HttpServletRequest request) {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		result.setCode(-1);
		result.setMessage("获取基础属性配置失败！");
		try {
			result.setData(PropConfigHandler.getBasePropConfig());
			result.setCode(0);
			result.setMessage("获取基础属性配置成功！");
		} catch (Exception e) {
			log.error("获取基础属性配置出错！", e);
		}
		return result;
	}

	/**
	 * 刷新数据
	 * 
	 * @author 朱建文 创建时间：2018年4月14日 上午10:17:51
	 */
	@ResponseBody
	@RequestMapping("/refreshConfig")
	public Result<String> refreshConfigToContant(){
		Result<String> result = new Result<String>();
		result.setCode(-1);
		result.setMessage("刷新数据失败！");
		try {
			this.pubgBasePropsConfigService.refreshConfigToContant();
			result.setCode(0);
			result.setData("");
			result.setMessage("刷新数据成功！");
			
			// 短信配置置为null，需要重新获取
			SmsConstant.config = null;
		} catch (Exception e) {
			log.error("刷新数据出错！", e);
		}
		return result;
	}
}
